<?php
declare (strict_types = 1);

namespace app\controller\backend;

use app\BaseController as AppBaseController;

class BaseController extends AppBaseController
{
    public $admin;
    public $lang;
    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [
//        'demo-fb',
        'login',
        'auth',
    ];

    public function initialize()
    {
        parent::initialize();
        self::createInstallFile();
        $this->admin = $this->request->admin;
        $this->lang = config('lang.allow_lang_list');
    }

    public function setLimit(): int
    {
        $limit = 10;
        $limitPage = (int)input('param.limit');
        if($limitPage){
            $limit = $limitPage;
        }
        return $limit;
    }

    private static function createInstallFile()
    {
        $installFile = app()->getRootPath().'data/install.lock';
        if(!file_exists($installFile) && file_exists(app()->getRootPath().'data/install/')){
            $url = env('app_host') . '/install';
            redirect($url);
        }else{
            touch($installFile);
        }
    }

    public function cors()
    {
        header("access-control-allow-headers: Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,Login-Agent,X-Mx-ReqToken,X-Requested-With");
        header("access-control-allow-methods: GET, POST, PUT, DELETE, HEAD, OPTIONS,PATCH");
        header("access-control-allow-credentials: true");
        header("access-control-allow-origin: *");
    }
}
